package com.tea.room.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class door {

	/**
	 * 平台登录
	 */
	public static String token() {
		String url = "http://dms.dmsiot.com/public/rest/third-party-app/token/get";
		String appid = "940881a8040d47e6b8f41d6c5f9b1600";
		String appsecret = "c86427670e4145b1a1d20f4afe34d637";
		String contentType = "application/json";
		HttpClient client = HttpClient.getInstance();
		Map<String, String> map = new HashMap<String, String>();
		map.put("appid", appid);// 登录用户名
		map.put("appsecret", appsecret);// 登录用户口令，填写密码
		String params = JsonUtil.mapToJsonObject(map).toJSONString();
		System.out.println("token url:" + url);
		System.out.println("token params:" + params);
		String post = client.sendHttpPost(url, params, contentType);
		String token = (String) JsonUtil.jsonStrToJsonObject(post).get("token");
		System.out.println("token:" + token);
		return token;
		
	}
	
	/**
	 * 获取楼栋列表
	 */
	public static void building() {
		String token = token();
		String url = "http://dms.dmsiot.com/rest/place/building/list?token=" + token;
		String contentType = "application/json";
		HttpClient client = HttpClient.getInstance();
		Map<String, String> map = new HashMap<String, String>();
		String params = JsonUtil.mapToJsonObject(map).toJSONString();
		System.out.println(params);
		String post = client.sendHttpPost(url, params, contentType);
		System.out.println(post);
	}
	
	
	/**
	 * 获取楼栋列表
	 */
	public static void floor() {
		String token = token();
		String url = "http://dms.dmsiot.com/rest/place/floor/list?token=" + token;
		String contentType = "application/json";
		HttpClient client = HttpClient.getInstance();
		Map<String, String> map = new HashMap<String, String>();
		String params = JsonUtil.mapToJsonObject(map).toJSONString();
		String post = client.sendHttpPost(url, params, contentType);
		System.out.println(url);
		System.out.println(post);
	}
	
	/**
	 * 获取房间列表
	 */
	public static void room() {
		String token = token();
		String url = "http://dms.dmsiot.com/rest/place/room/list?token=" + token;
		String contentType = "application/json";
		HttpClient client = HttpClient.getInstance();
		Map<String, String> map = new HashMap<String, String>();
		String params = JsonUtil.mapToJsonObject(map).toJSONString();
		System.out.println(url);
		String post = client.sendHttpPost(url, params, contentType);
		System.out.println(post);
	}
	
	/**
	 * 获取房间id
	 */
	public static void roomget() {
		String token = token();
		String url = "http://dms.dmsiot.com/rest/place/room/get?token=" + token;
		String contentType = "application/json";
		HttpClient client = HttpClient.getInstance();
		Map<String, String> map = new HashMap<String, String>();
		map.put("building", "2");// 楼栋号
		map.put("floor", "10");// 楼层号
		map.put("room", "");// 房间号
		String params = JsonUtil.mapToJsonObject(map).toJSONString();
		System.out.println(url);
		String post = client.sendHttpPost(url, params, contentType);
		System.out.println(post);
	}
	
	/**
	 * 获取门锁列表
	 */
	public static void doorlock() {
		String token = token();
		String url = "http://dms.dmsiot.com/rest/doorlock/list?token=" + token;
		String contentType = "application/json";
		HttpClient client = HttpClient.getInstance();
		Map<String, String> map = new HashMap<String, String>();
		map.put("placeId", "5677");// 房间id
		String params = JsonUtil.mapToJsonObject(map).toJSONString();
		System.out.println(url);
		String post = client.sendHttpPost(url, params, contentType);
		System.out.println(post);
	}
	
	/**
	 * 查询门锁电池和信号状态
	 */
	public static void battery() {
		String token = token();
		String url = "http://dms.dmsiot.com/rest/doorlock/query-lock-battery?token=" + token;
		String contentType = "application/json";
		HttpClient client = HttpClient.getInstance();
		Map<String, Object> map = new HashMap<String, Object>();
		List<String> list = new ArrayList<String>();
		map.put("roomIds", list);
		String params = JsonUtil.mapToJsonObject(map).toJSONString();
		System.out.println("url:" + url);
		System.out.println("params:" + params);
		String post = client.sendHttpPost(url, params, contentType);
		System.out.println(post);
	}
	
	/**
	 * 远程开锁
	 */
	public static void unlock() {
		String token = token();
		String url = "http://dms.dmsiot.com/rest/doorlock/remote-unlock?token=" + token;
		String contentType = "application/json";
		HttpClient client = HttpClient.getInstance();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("lockId", 372);// 门锁id
		map.put("hardwareId", "5C9F539A");// 门锁硬件id
		String params = JsonUtil.mapToJsonObject(map).toJSONString();
		System.out.println("url:" + url);
		System.out.println("params:" + params);
		String post = client.sendHttpPost(url, params, contentType);
		System.out.println(post);
	}
	
	/**
	 * 远程开锁   查询操作结果
	 */
	public static void operation() {
		String token = token();
		String url = "http://dms.dmsiot.com/rest/doorlock/query-operation-status?token=" + token;
		String contentType = "application/json";
		HttpClient client = HttpClient.getInstance();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("requestType", 1);// 请求类型，1=下发在线密码，2=在线卡片，3=删除在线密码，4=删除在线卡片，5=远程开锁，6=远程锁定，7=远程解锁，8=设置常开模式，9=设置非常开模式
		map.put("operationId", "486fe9eb-1389-4fc7-80b5-3c2c0f3cbd82");// 操作id
		map.put("hardwareId", "5C9F539A");// 门锁硬件id
		String params = JsonUtil.mapToJsonObject(map).toJSONString();
		System.out.println("url:" + url);
		System.out.println("params:" + params);
		String post = client.sendHttpPost(url, params, contentType);
		System.out.println(post);
	}
	
	/**
	 * 下发在线密码
	 */
	public static void create() {
		String token = token();
		String url = "http://dms.dmsiot.com/rest/doorlock/online-password-key/create?token=" + token;
		String contentType = "application/json";
		HttpClient client = HttpClient.getInstance();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("lockId", 372);// 门锁id，通过房间id（根据楼栋房号信息查询获取）查询门锁列表获取
		map.put("validStart", "1555549522000");// 有效开始时间，时间戳
		map.put("validUntil", "1555549522000");// 有效结束时间，时间戳
		map.put("holder", "张三");// 持有人，可选
		map.put("canOpenAntilock", "true");// 是否可以开反锁,true：可以开,false:不能开
		map.put("phoneCountry", "+86");// 手机国际区号，国内为+86，其他国家按照相应区号填写
		map.put("phoneNumber", "18925910952");// 手机号
		map.put("password", "456456");// 密码，长度4-8位，一般为6位
		map.put("subType", "4");// 密码类型， 1=永久，2=单次，4=限时
		String params = JsonUtil.mapToJsonObject(map).toJSONString();
		System.out.println("url:" + url);
		System.out.println("params:" + params);
		String post = client.sendHttpPost(url, params, contentType);
		System.out.println(post);
	}
	
	/**
	 * 查询下发在线密码结果
	 */
	public static void querycreate() {
		String token = token();
		String url = "http://dms.dmsiot.com/rest/doorlock/query-operation-status?token=" + token;
		String contentType = "application/json";
		HttpClient client = HttpClient.getInstance();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("requestType", 1);// 请求操作的类型，下发在线密码为1
		map.put("operationId", "ba9770d0-c63e-4c94-a695-81796e94d175");// 操作id，下发在线密码时返回
		map.put("hardwareId", "5C9F539A");// 门锁的硬件id
		String params = JsonUtil.mapToJsonObject(map).toJSONString();
		System.out.println("url:" + url);
		System.out.println("params:" + params);
		String post = client.sendHttpPost(url, params, contentType);
		System.out.println(post);
	}
	
	public static void main(String[] args) {
	    floor() ;
	}
}
